package com.personnel.controller;


import com.personnel.util.GetAndPost;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

/**
 *@author yuepeng
 * @since 2021-12-28 13:55:41
 * @version 1.0
 */
@RestController
@RequestMapping("/weather")
public class WeatherController {
    @GetMapping("/select")
    public Map<String, Object> select(@RequestParam(required = false, defaultValue = "成都") String cityName) throws UnsupportedEncodingException {

        String encodeStr = URLEncoder.encode(cityName, "utf-8");
        //System.out.println("处理后:" + encodeStr);
        String url = "http://apis.juhe.cn/simpleWeather/query?city="+ encodeStr +"&key=46e2cb7aa8ccaadf977a025b3814b147";

        String s = GetAndPost.requestGet(url);

        JSONObject jsonObject = new JSONObject(s);
        String result = jsonObject.get("result").toString();
        JSONArray jsonArray = new JSONArray("["+result+"]");
        jsonObject.put("result",jsonArray);
        jsonObject.put("code", 0);
        jsonObject.put("count", 1);
        return jsonObject.toMap();
    }
}
